package com.nanjustar.web.controller.security;

import com.nanjustar.api.moudle.security.api.ConsumerService;
import com.nanjustar.api.moudle.security.dto.ConsumerInfoDto;
import com.nanjustar.api.moudle.security.vo.ConsumerConditionVo;
import com.nanjustar.api.moudle.security.vo.ConsumerVo;
import com.nanjustar.business.annotation.Log;
import com.nanjustar.common.constant.LogConst;
import com.nanjustar.common.enums.RoleLevelEnum;
import com.nanjustar.common.result.ResponseResult;
import com.nanjustar.common.utils.BeanCopyUtil;
import com.nanjustar.mapper.utils.ConsumerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author nanjustar
 * @since 2021-11-13
 */
@RestController
@Api(tags = "获取用户信息")
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    /**
     * 获取当前用户信息
     *
     * @return {@link ConsumerInfoDto} 当前用户信息
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/getUserInfo")
    public ResponseResult<ConsumerInfoDto> getUserInfo() {
        ConsumerInfoDto consumerInfo = BeanCopyUtil.copyObject(ConsumerUtil.getUserDetails(), ConsumerInfoDto.class);
        consumerInfo.setConsumerLevel(RoleLevelEnum.getGenderNameByCode(consumerInfo.getRoleList()));
        return ResponseResult.success("用户信息获取成功！", consumerInfo);
    }


    /**
     * 查询用户路由列表
     *
     * @return {@link ConsumerInfoDto} 菜单路由信息
     */
    @ApiOperation(value = "查询用户路由列表")
    @GetMapping("/consumer/consumerRouterList")
    public ResponseResult<List<ConsumerInfoDto>> consumerRouterList(){
        return ResponseResult.success("用户路由列表获取成功！",consumerService.listConsumerRouter());
    }


    /**
     * 查询用户路由列表
     *
     * @return {@link ConsumerInfoDto} 菜单路由信息
     */
    @ApiOperation(value = "条件查询用户信息")
    @GetMapping("/consumer/listConsumerByCondition")
    public ResponseResult<List<ConsumerInfoDto>> listConsumerByCondition(ConsumerConditionVo consumerConditionVo){
        return ResponseResult.success("用户路由列表获取成功！",consumerService.listConsumerByCondition(consumerConditionVo));
    }


    /**
     * 新增菜单信息
     *
     * @param consumerVo 用户信息 vo类
     * @return {@link ResponseResult} 用户新增状态
     */
    @ApiOperation(value = "新增用户")
    @Log(logType = LogConst.LOG_TYPE_SAVE)
    @PostMapping("/consumer/saveConsumer")
    public ResponseResult<String> saveConsumer(@RequestBody @Valid ConsumerVo consumerVo){
        consumerService.saveConsumer(consumerVo);
        return ResponseResult.success("用户新增成功！");
    }



    /**
     * 修改用户信息
     *
     * @param consumerVo 用户信息 vo类
     * @return {@link ResponseResult} 用户修改状态
     */
    @ApiOperation(value = "修改用户信息")
    @Log(logType = LogConst.LOG_TYPE_UPDATE)
    @PutMapping("/consumer/updateConsumer")
    public ResponseResult<String> updateMenu(@RequestBody @Valid ConsumerVo consumerVo) {
        consumerService.updateConsumer(consumerVo);
        return ResponseResult.success("用户修改成功！");
    }


    /**
     * 删除用户
     *
     * @param id 用户信息 vo类
     * @return {@link ResponseResult} 用户删除状态
     */
    @ApiOperation(value = "删除用户")
    @Log(logType = LogConst.LOG_TYPE_SAVE)
    @DeleteMapping("/consumer/deleteConsumer/{id}")
    public ResponseResult<String> deleteConsumer(@PathVariable Integer id){
        consumerService.deleteConsumer(id);
        return ResponseResult.success("删除用户成功");
    }
}

